#pragma once
#include <sys/epoll.h>
#include <cstring>
class Epoller
{
    const static int size = 88;
    const static int default_val = -1;
public:
    Epoller() :_epfd(default_val)
    {}
    ~Epoller()
    {
        if(_epfd != default_val)
            close(_epfd);
    }
    void create()
    {
        _epfd = epoll_create(size);
        if(_epfd == -1)
        {
            logMessage(FATAL, "epoll_create failed err code: %d errstring: %s", strerror(errno));
            exit(EPOLL_CREATE_ERR);
        }
        else
        {
            logMessage(NORMAL, "epoll_create success fd: %d", _epfd);
        }
    }

    void ctl(int sock, int op, uint32_t event)
    {
        struct epoll_event events;
        events.data.fd = sock;
        events.events = event;
        int n = epoll_ctl(_epfd, op, sock, &events);
        if(n != 0)
        {
            logMessage(ERROR, "epoll_ctl err, code: %d, errstring: %s", errno, strerror(errno));
        }
    }
    
    int wait(struct epoll_event* rcvs, int num , int timeout)
    {
        int n = epoll_wait(_epfd, rcvs, num, timeout);
        return n;
    }

private:
    int _epfd;

};